﻿namespace Nello.Web
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Web.UI.WebControls;

    public class TreeViewWrapper
    {
        private DataTable datasource;
        private string fieldNames;
        private string parentIdField;
        private string rootId;
        private string textField;
        private System.Web.UI.WebControls.TreeView tvw;
        private string urlField;
        private string valueField;

        public TreeViewWrapper()
        {
            this.rootId = "0";
        }

        public TreeViewWrapper(System.Web.UI.WebControls.TreeView treeView, DataTable datasource)
        {
            this.rootId = "0";
            this.tvw = treeView;
            this.datasource = datasource;
        }

        private void InitTree(string parentid, TreeNodeCollection nodes)
        {
            DataView dv = new DataView();
            if (this.datasource.TableName == "")
            {
                this.datasource.TableName = "NewTable";
            }
            dv.Table = this.datasource;
            dv.RowFilter = this.parentIdField + "='" + parentid + "'";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Value = drv[this.valueField].ToString();
                node.Text = drv[this.textField].ToString();
                if (this.urlField != "")
                {
                    node.NavigateUrl = drv[this.urlField].ToString();
                }
                nodes.Add(node);
                this.InitTree(node.Value, node.ChildNodes);
            }
        }

        public void Render()
        {
            this.tvw.Nodes.Clear();
            string[] ss = this.fieldNames.Split(new char[] { ',' });
            if (ss.Length < 3)
            {
                throw new Exception("FieldNames属性设置的格式为parentid,id,text,url或parentid,id,text");
            }
            this.parentIdField = ss[0];
            this.valueField = ss[1];
            this.textField = ss[2];
            if (ss.Length == 3)
            {
                this.urlField = "";
            }
            else
            {
                this.urlField = ss[3];
            }
            this.InitTree(this.rootId, this.tvw.Nodes);
        }

        public static void SetChecked(TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                bool bb = false;
                if (node.ChildNodes.Count == 0)
                {
                    bb = node.Checked;
                }
                foreach (TreeNode nod in node.ChildNodes)
                {
                    if (nod.Checked)
                    {
                        bb = true;
                        break;
                    }
                }
                node.Checked = bb;
                SetChecked(node.ChildNodes);
            }
        }

        public static void SetNodesChecked(TreeNodeCollection nodes, IList nodevalues)
        {
            foreach (TreeNode node in nodes)
            {
                if (nodevalues.Contains(node.Value))
                {
                    node.Checked = true;
                }
                SetNodesChecked(node.ChildNodes, nodevalues);
            }
        }

        public static void UnCheckedAll(TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                node.Checked = false;
                UnCheckedAll(node.ChildNodes);
            }
        }

        public DataTable Datasource
        {
            get
            {
                return this.datasource;
            }
            set
            {
                this.datasource = value;
            }
        }

        public string FieldNames
        {
            get
            {
                return this.fieldNames;
            }
            set
            {
                this.fieldNames = value;
            }
        }

        public string RootId
        {
            get
            {
                return this.rootId;
            }
            set
            {
                this.rootId = value;
            }
        }

        public System.Web.UI.WebControls.TreeView TreeView
        {
            get
            {
                return this.tvw;
            }
            set
            {
                this.tvw = value;
            }
        }
    }
}

